package isg3.data;

import isg3.domain.Libro;
import isg3.domain.Usuario;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class JDBCLibroDAO implements ILibroDAO {
private Connection conn;
	
	public JDBCLibroDAO(){
		conn = ConnectionManager.getInstance().checkOut();
	}
	public Libro select(String OIDLibro) {
		PreparedStatement stmt = null;
		Libro temp = null;
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM libros WHERE (OIDlibro = ?)";
            stmt = conn.prepareStatement(sql); //conn es una conexi�n v�lida
            stmt.setString(1, OIDLibro);
            stmt.executeQuery();
            result = stmt.executeQuery();

            result.next();
            	temp = new Libro();
                temp.setTitulo(result.getString("titulo"));
                temp.setAutor(result.getString("autor"));
                temp.setEstadoLibro(result.getString("estado"));
	            temp.setCategoria(result.getString("categoria"));
	            temp.setEditorial(result.getString("editorial"));
	            temp.setISBN(result.getString("ISBN"));
	            temp.setIdLibro(result.getString("OIDLibro"));
          
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return temp;
	}

	public List selectAllLibro() {
		PreparedStatement stmt = null;
        List searchResults = new LinkedList();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM libros"; //WHERE (tipo= tipoU)";
            stmt = conn.prepareStatement(sql); //conn es una conexi�n v�lida
            //stmt.setString(4, tipo);
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
            	Libro temp = new Libro();
                temp.setTitulo(result.getString("titulo"));
                temp.setAutor(result.getString("autor"));
                temp.setEstadoLibro(result.getString("estado"));
	            temp.setCategoria(result.getString("categoria"));
	            temp.setEditorial(result.getString("editorial"));
	            temp.setISBN(result.getString("ISBN"));
	            temp.setIdLibro(result.getString("OIDLibro"));
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
	}

}
